package com.example.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMqConfig {

    //业务队列
    @Bean
    public Queue queueBiz() {
        Map<String, Object> arguments = new HashMap<>();
        // 设置队列的TTL
        arguments.put("x-message-ttl",30*1000);
        // 设置队列的空闲存活时间（如该队列根本没有消费者，一直没有使用，队列可以存活多久）
        arguments.put("x-expires", 60*60*1000);
        //指定死信队列的交换器和路由键
        arguments.put("x-dead-letter-exchange","exchange.order.dlx");
        arguments.put("x-dead-letter-routing-key","rk.order.dlx");

        return new Queue("queue.order", false, false, false, arguments);
    }

    //死信队列
    @Bean
    public Queue queueDlx() {
        Map<String, Object> arguments = new HashMap<>();
        return new Queue("queue.order.dlx", false, false, false, arguments);
    }

    //业务交换器
    @Bean
    public Exchange exchangeBiz() {
        return new DirectExchange("exchange.order", false, false, null);
    }

    //死信交换器
    @Bean
    public Exchange exchangeDlx() {
        return new DirectExchange("exchange.order.dlx", false, false, null);
    }

    //业务交换器绑定业务队列
    @Bean
    public Binding bindingBiz() {
        return new Binding("queue.order", Binding.DestinationType.QUEUE, "exchange.order", "rk.order", null);
    }

    //死信交换器绑定死信队列
    @Bean
    public Binding bindingDlx() {
        return new Binding("queue.order.dlx", Binding.DestinationType.QUEUE, "exchange.order.dlx", "rk.order.dlx", null);
    }

}
